import Vue from 'vue';
import './../../styles/tables.scss'
import router from './../../router'



function add(setting) {
  //new Promise(function (resolve, reject) {

    const div = document.createElement('div');

    document.body.appendChild(div);
   
      var vm = new Vue({
        el: div,
        template: `<el-dialog title="添加下游政策" :visible.sync="isshow" width="550px" :close-on-click-modal='false'>
        <div class="content">
          <el-form :model="json" :rules="rules" ref="json" label-width="150px">
            <el-form-item label="添加对象类型:" :label-width="formLabelWidth">
  
              部门
            </el-form-item>
            <el-form-item label="添加对象" :label-width="formLabelWidth" prop="targetId">
              <el-select no-data-text="无部门可选" class="inputs" multiple v-model="json.targetId" size="mini" placeholder="请选择对象">
                <el-option v-for="item in organization" :label="item.organizationName" :value="item.organizationId" :key="item.organizationId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="交强险设置:" :label-width="formLabelWidth">
              <div class="numcontent" :class="indexs==1?'actives':''">
                <span>
                  佣金
                  <el-input class="numinp" :class="{'mmerror': adjiaoqiang}" v-model="json.lowerBrokerageTraffic" @change="adjiaoqiang1($event,'lowerBrokerageTraffic')" size="mini"></el-input>
                  <span> {{json.brokerageBusinessType | isPercentage}}</span>
                </span>
  
                <span>
                  奖励
                  <el-input class="numinp" :class="{'mmerror': adjiaoqiang}" v-model="json.lowerRewardTraffic" @change="adjiaoqiang2($event,'lowerRewardTraffic')" size="mini"></el-input>
                  <span> {{json.brokerageBusinessType | isPercentage}}</span>
                </span>
  
              </div>
            </el-form-item>
  
            <el-form-item label="商业险设置:" :label-width="formLabelWidth">
              <div class="numcontent" :class="indexs_tow==2?'actives_tuo':''">
                <span>
                  佣金
                  <el-input class="numinp" :class="{'mmerror': adshangye}" v-model="json.lowerBrokerageBusiness" @change="adshangye1($event,'lowerBrokerageBusiness')" size="mini"></el-input>
                  <span> {{json.brokerageBusinessType | isPercentage}}</span>
                </span>
  
                <span>
                  奖励
                  <el-input class="numinp" :class="{'mmerror': adshangye}" v-model="json.lowerRewardBusiness" @change="adshangye2($event,'lowerRewardBusiness')" size="mini"></el-input>
                  <span> {{json.brokerageBusinessType | isPercentage}}</span>
                </span>
  
              </div>
            </el-form-item>
            <el-form-item class="btns">
  
              <el-button size="small" type="primary" :loading="loading" @click="submits('json')">确 定</el-button>
            </el-form-item>
  
          </el-form>
        </div>
      </el-dialog>`,
        data() {
          return {
            loading:false,
            adjiaoqiang: false,
            adshangye: false,
            shangye: false,
            jiaoqiang: false,
            isdelete: false,
            iseditshow: false, //编辑弹框
            ruleForm: {
              dismissalSeason: null
            },
            ruless: {
              dismissalSeason: [
                {
                  required: true,
                  message: "请输入驳回原因",
                  trigger: "blur"
                }
              ]
            },
            Rejectshow: false,
            dialogTableVisible: false,
            ex_dialogTableVisible: false,
            existence: [],
            rovalHtml: "提交审核",
            isshows: false,
            id: null, //当前id
            policylist: [],
            options: [],
            uppoliy: [],
            workoptions: [],
            info: {},
            navnum: 2,
            dialogShow: false,
            //dialog
            isshow: false,
            formLabelWidth: "120px",
            objtypelist: [
              //对象列表
              //					{name:"政策",value:"0"},
              {
                name: "组织",
                value: "1"
              },
              {
                name: "用户",
                value: "2"
              }
            ],
            premiumlist: [
              //保费列表
              {
                name: "按净保费",
                value: 1
              },
              {
                name: "按全保费",
                value: 0
              }
            ],
            Maidlist: [
              //保费列表
              {
                name: "百分比",
                value: 0
              },
              {
                name: "人名币",
                value: 1
              }
            ],
            Pointslist: [
              //扣点
              {
                name: "是",
                value: 1
              },
              {
                name: "否",
                value: 0
              }
            ],
            addobj: [
              //添加对象列表
            ],
            rule: {
              batchName: [
                {
                  required: true,
                  message: "请输入批次名称",
                  trigger: "blur"
                },
                {
                  min: 1,
                  max: 50,
                  message: "长度在 1 到 50个字符",
                  trigger: "blur"
                }
              ]
            },
            rules: {
              extendType: [
                {
                  required: true,
                  message: "请选择对象类型",
                  trigger: "change"
                }
              ],
              targetId: [
                {
                  required: true,
                  message: "请选择对象",
                  trigger: "change"
                }
              ],
              premiumType: [
                {
                  required: true,
                  message: "请选择保费类型",
                  trigger: "change"
                }
              ],
              brokerageBusinessType: [
                {
                  required: true,
                  message: "请选择返佣类型",
                  trigger: "change"
                }
              ],
              chargeType: [
                {
                  required: true,
                  message: "请选择是否扣点",
                  trigger: "change"
                }
              ]
            },
            json: {
              policyItemId: "", //政策id
              extendType: "1", //对象类型,
              targetId: [], //对象
              lowerBrokerageTraffic: "0", //交强-佣金
              lowerRewardTraffic: "0", //较强——奖励
              lowerBrokerageBusiness: "0", //商业-佣金
              lowerRewardBusiness: "0", //商业——奖励
              lowerBrokerageVehicleTax: "0" //下游车船税佣金比例
              //	remarks: "" //备注
            },
            userlist: [],
            treeData: [], //树数据
            treeProps: {
              label: "displayName",
              children: "orgs",
              level: "depth",
              value: "id"
            }, //树属性
            copyinfo: {
              staffId: []
            },
            islimit: false,
            policyId: null,
            Jurisdiction: "",
            indexs: null,
            indexs_tow: null,
            additional: {},
            organization: [],
            dds: {
              aa: "1"
            },
            isdelete: false
          };
        },
        filters: {
          isPercentage(val) {
            if (val == 0) {
              return "%";
            } else {
              return "元";
            }
          }
        },
        mounted() {
            this.isshow = true
               this.json.policyItemId = setting.policyItemId;
            this.json.lowerBrokerageTraffic = '0';
            this.json.lowerRewardTraffic = '0';
            this.json.lowerBrokerageBusiness = '0';
            this.json.lowerRewardBusiness = '0';
            Vue.set(this.json, "brokerageBusinessType", setting.brokerageBusinessType);

            let params = {
              id: setting.policyItemId
            };
            this.post(params, "services/app/policy/ChildOrganizations").then(res => {
              console.log(res);
              if (res.success) {
                this.organization = res.result;
              } else {
                this.$notify.error({
                  title: "错误",
                  message: res.error.message
                });
              }
            });
        },
        methods: {
          //数字小数点
          clearNoNum(obj, max) {
            obj = obj.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
            obj = obj.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
            obj = obj
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
            if (obj.indexOf(".") < 0 && obj != "") {
              //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
              obj = parseFloat(obj);
            }
            if (max && obj > max) {
              obj = max;
            }
            if (obj == "") {
              obj = 0;
            }
            console.log(obj);
            return obj;
          },
       
          //添加下游交强佣金奖励
          adjiaoqiang1(event, name) {
            let obj;
            if (this.json.brokerageBusinessType == 0) {
              let max = 100;
              obj = this.clearNoNum(event, max);
            } else {
              obj = this.clearNoNum(event);
            }
            this.json.lowerBrokerageTraffic = obj;
          },
          adjiaoqiang2(event, name) {
            let obj;
            if (this.json.brokerageBusinessType == 0) {
              let max = 100;
              obj = this.clearNoNum(event, max);
            } else {
              obj = this.clearNoNum(event);
            }
            this.json.lowerRewardTraffic = obj;
          },

          //添加下游商业佣金奖励
          adshangye1(event, name) {
            console.log(this.json.brokerageBusinessType)
            let obj;
            if (this.json.brokerageBusinessType == 0) {
              let max = 100;
              obj = this.clearNoNum(event, max);
            } else {
              obj = this.clearNoNum(event);
            }
            this.json.lowerBrokerageBusiness = obj;
          },
          adshangye2(event, name) {
            let obj;
            if (this.json.brokerageBusinessType == 0) {
              let max = 100;
              obj = this.clearNoNum(event, max);
            } else {
              obj = this.clearNoNum(event);
            }
            this.json.lowerRewardBusiness = obj;
          },
          //添加下游确定
          submits(formName) {
            console.log(this.json);
            this.loading = true;
            let lowerBrokerageTraffic = Number(this.json.lowerBrokerageTraffic);
            let lowerRewardTraffic = Number(this.json.lowerRewardTraffic);
            let lowerBrokerageBusiness = Number(this.json.lowerBrokerageBusiness);
            let lowerRewardBusiness = Number(this.json.lowerRewardBusiness);
            this.$refs[formName].validate((valid) => {
              if (valid) {
                //0是百分比 1是元
                if (this.json.brokerageBusinessType == 0) {
                  if (lowerBrokerageTraffic + lowerRewardTraffic > 100) {
                    this.adjiaoqiang = true;
                  } else {
                    this.adjiaoqiang = false;
                  }
                  if (lowerBrokerageBusiness + lowerRewardBusiness > 100) {
                    this.adshangye = true;
                  } else {
                    this.adshangye = false;
                  }
                  if (lowerBrokerageTraffic + lowerRewardTraffic <= 100 && lowerBrokerageBusiness + lowerRewardBusiness <= 100) {
                    this.json.targetId = this.json.targetId.join(",")        
                    let arr = [];
							arr.push(this.json);
							let params = {
								"extends": arr
							}
							this.post(params, "services/app/policy/PolicyAddExtendInfo").then(rs => {
                                if(rs.success){
                                    this.isshow = false;
                                    this.loading = false;
                                    setting.success()
                                    this.$notify({
                                        title: '提交成功',
                                        message: '您已成功创建一条下游政策',
                                        type: 'success',
                                        customClass:"el-notification-success"
                                      });
                                }else{
                                    this.loading = false;
                                }
								
								
							});
						
                  }
                } else {//类型是元直接提交
                    this.json.targetId = this.json.targetId.join(",")
                    let arr = [];
                    arr.push(this.json);
                    let params = {
                        "extends": arr
                    }
                    this.post(params, "services/app/policy/PolicyAddExtendInfo").then(rs => {
                        if(rs.success){
                            this.isshow = false;
                            this.loading = false;
                            setting.success()
                            this.$notify({
                                title: '提交成功',
                                message: '您已成功创建一条下游政策',
                                type: 'success',
                                customClass:"el-notification-success"
                              });
                        }else{
                            this.loading = false;
                        }
                        
                        
                    });

                }
              } else {
                console.log("验证为同构")
              }

            })

            // let arr = [];
            // arr.push(this.json);
            // let params = {
            //   extends: arr
            // };
            // this.post(params, "services/app/policy/PolicyAddExtendInfo").then(rs => {
            //   console.log(rs);
            //   this.$notify({
            //     title: "提交成功",
            //     message: "您已成功创建一条下游政策",
            //     type: "success",
            //     customClass: "el-notification-success"
            //   });
            //   system();
            // });
          },
       
          addlowpolic(policyItemId, brokerageBusinessType) {
            this.isshow = true;
            this.json.policyItemId = policyItemId;
            this.json.lowerBrokerageTraffic = '0';
            this.json.lowerRewardTraffic = '0';
            this.json.lowerBrokerageBusiness = '0';
            this.json.lowerRewardBusiness = '0';
            Vue.set(this.json, "brokerageBusinessType", brokerageBusinessType);
            // this.json = Object.assign({}, this.json, {
            //   chargeType: chargeType,
            //   policyId: policyId,
            //   upperBrokerageTraffic: upperBrokerageTraffic,
            //   upperRewardTraffic: upperRewardTraffic,
            //   upperBrokerageBusiness: upperBrokerageBusiness,
            //   upperRewardBusiness: upperRewardBusiness
            // });
            // Vue.set(this.json, "policyItemId", val.policyId)
            // 	Vue.set(this.json, "lowerBrokerageTraffic", 0)
            // 	Vue.set(this.json, "lowerRewardTraffic", 0)
            // 	Vue.set(this.json, "lowerBrokerageBusiness", 0)
            // 	Vue.set(this.json, "lowerRewardBusiness", 0)
            // 	Vue.set(this.json, "lowerBrokerageVehicleTax", 0)
            let params = {
              id: policyItemId
            };
            this.post(params, "services/app/policy/ChildOrganizations").then(res => {
              console.log(res);
              if (res.success) {
                this.organization = res.result;
              } else {
                this.$notify.error({
                  title: "错误",
                  message: res.error.message
                });
              }
            });
          },
        
     
         
        }
      })

  





  //})
}
export default add
